﻿using System.Collections;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System;

namespace HtmlMaker
{
    public class PageBuilder
    {
        public static Hashtable ds
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlPath"></param>
        /// <param name="datas"></param>
        /// <returns></returns>
        public static string Format(string htmlPath, Hashtable datas, ListViewItem lvItem)
        {
            TextReader r = new StreamReader(htmlPath, Encoding.UTF8);
            string fcontent = r.ReadToEnd();
            r.Close();

            // 去掉示范数据 
            fcontent = Regex.Replace(fcontent, @"#ts:\([^\)]+\)|\n|\r|\t", "", RegexOptions.Compiled);

            // 分割
            string[] sections = Regex.Split(fcontent, @"(?:<!-- ts:([^>]+) -->)", RegexOptions.Compiled);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < sections.Length; i++)
            {
                switch (sections[i])
                {
                    case "list-item=start":
                        string dsName = "", _name = "";
                        Hashtable[] _ds = null;
                        while (sections[++i] != "list-item=end")
                        {
                            string sct = sections[i].Trim();
                            if (!string.IsNullOrEmpty(sct))
                            {
                                if (sct.StartsWith("data="))
                                {
                                    dsName = sct.Substring(5, sct.Length - 5);
                                    _ds = ds[dsName] as Hashtable[];
                                    break;
                                }
                            }
                        }
                        int j = i, k = 0;
                        for (int t = 0; t < _ds.Length; t++)
                        {
                            while (sections[++i] != "list-item=end")
                            {
                                string sct = sections[i].Trim();
                                if (!string.IsNullOrEmpty(sct))
                                {
                                    if (sct.StartsWith("binding="))
                                    {
                                        _name = sct.Substring(8, sct.Length - 8);
                                        sb.Append(_ds[t][_name]);
                                    }
                                    else
                                    {
                                        sb.Append(sct);
                                    }
                                }
                            }
                            k = i;
                            i = j;
                        }
                        i = k;
                        break;

                    default:
                        sb.Append(sections[i]);
                        break;
                }
            }

            return sb.ToString();
        }

        private static string Format(string[] sections, Hashtable dataSource)
        {
            StringBuilder sb = new StringBuilder();

            return sb.ToString();
        }

        //public static void FileStreamWriteFile(string path, string content)
        //{
        //    byte[] byData;
        //    char[] charData;
        //    try
        //    {
        //        FileStream nFile = new FileStream(path, FileMode.Create);
        //        //获得字符数组
        //        charData = content.ToCharArray();
        //        //初始化字节数组
        //        byData = new byte[charData.Length];
        //        //将字符数组转换为正确的字节格式
        //        Encoder enc = Encoding.UTF8.GetEncoder();
        //        enc.GetBytes(charData, 0, charData.Length, byData, 0, true);
        //        nFile.Seek(0, SeekOrigin.Begin);
        //        nFile.Write(byData, 0, byData.Length);
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}
    }
}
